package com.lm.poitl.controller;

import com.lm.poitl.service.WordGeneratorService;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;

@RestController
@RequestMapping("/word")
public class WordController {

    private final WordGeneratorService wordService;

    public WordController(WordGeneratorService wordService) {
        this.wordService = wordService;
    }

    @GetMapping("/download")
    public ResponseEntity<InputStreamResource> downloadWord() throws Exception {
        // 生成 Word 文件
        wordService.generateWord();

        // 读取文件
        File file = new File("output.docx");
        FileInputStream fis = new FileInputStream(file);

        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=output.docx")
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(new InputStreamResource(fis));
    }
}